﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
          
        }

        private void btnEqual_Click(object sender, EventArgs e)
        {
            float r; 

            string t = txtResult.Text; 

            int space = t.IndexOf(" "); 

            string s1 = t.Substring(0, space); 

            char op = Convert.ToChar(t.Substring(space + 1, 1)); 

            string s2 = t.Substring(space + 3); 

            float arg1 = Convert.ToSingle(s1);

            float arg2 = Convert.ToSingle(s2);

            switch (op)

            {

                case '+':

                    r = arg1 + arg2;

                    break;

                case '-':

                    r = arg1 - arg2;

                    break;

                case '*':

                    r = arg1 * arg2;

                    break;

                case '/':

                    if (arg2 == 0)

                    {

                        throw new ApplicationException();



                    }

                    else

                    {

                        r = arg1 / arg2;

                        break;

                    }

                    break;

                default:

                    throw new ApplicationException();

            }

            txtResult.Text = t+" "+"="+" "+r.ToString();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textResult_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtResult_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text += btn.Text;
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text = txtResult.Text + " " + btn.Text + " ";
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text = txtResult.Text + " " + btn.Text + " ";
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text = txtResult.Text + " " + btn.Text + " ";
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            txtResult.Text = txtResult.Text + " " + btn.Text + " ";
        }
    }
}
